from tkinter import *
import pymysql

class GTPort:

    def __init__(self, win):

        self.loginWin = win

        self.loginWin.title("GTPort")

        # Create labels
        
        l1 = Label(self.loginWin, text = "Username", width = 10)
        l2 = Label(self.loginWin, text = "Password", width = 10)

        # Create string variables

        self.username = StringVar()
        self.password = StringVar()

        # Create entry boxes, linking string variables to entries

        e1 = Entry(self.loginWin, textvariable = self.username, width = 30)
        e2 = Entry(self.loginWin, textvariable = self.password, width = 30)

        # When click create account button, register method called

        b1 = Button(self.loginWin, text = "Create Account", width = 15, command = self.register)

        # When click login button, login method called
        
        b2 = Button(self.loginWin, text = "Login", width = 15, command = self.login)

        # GUI Formatting

        l1.grid(column = 0, row = 0, sticky = E, padx = 2)
        l2.grid(column = 0, row = 1, sticky = E, padx = 2)
        e1.grid(column = 1, row = 0, pady = 2)
        e2.grid(column = 1, row = 1, pady = 2)
        b1.grid(column = 1, row = 2, sticky = E, padx = 5, pady = 2)
        b2.grid(column = 2, row = 2, padx = 5, pady = 2)

        # Define windows

        self.regWin = Toplevel()
        self.facHome = Toplevel()
        self.studHome = Toplevel()
        self.studServicesWin = Toplevel()
        self.facServicesWin = Toplevel()
        self.studInfoWin = Toplevel()
        self.facInfoWin = Toplevel()
        

        # Hide windows

        self.regWin.withdraw()
        self.facHome.withdraw()
        self.studHome.withdraw()
        self.studServicesWin.withdraw()
        self.facServicesWin.withdraw()
        self.studInfoWin.withdraw()
        self.facInfoWin.withdraw()
        
    def register(self):

        # Hides login window and creates register window

        self.loginWin.withdraw()
        

        self.regWin.title("CREATE ACCOUNT")

        # Create labels

        l1 = Label(self.regWin, text = "Username")
        l2 = Label(self.regWin, text = "Password")
        l3 = Label(self.regWin, text = "Confirm Password")
        l4 = Label(self.regWin, text = "Type of User")

        # Create string variables to keep track of what is in entries/drop down selection

        self.regUsername = StringVar()
        self.regPassword = StringVar()
        self.regPasswordConf = StringVar()
        self.userType = StringVar()

        self.userType.set("Student")

        # Create entries

        e1 = Entry(self.regWin, textvariable = self.regUsername)
        e2 = Entry(self.regWin, textvariable = self.regPassword)
        e3 = Entry(self.regWin, textvariable = self.regPasswordConf)

        # Create drop down menu to select user type
        
        dropDown = OptionMenu(self.regWin, self.userType, "Student", "Faculty")

        # If click cancel, will call cancelRegister method, which hides register window and shows login window

        b1 = Button(self.regWin, text = "Cancel", command = self.cancelRegister)
 
        # If click register, will call insertNew method
        
        b2 = Button(self.regWin, text = "Register", command = self.insertNew)

        # GUI Formatting

        l1.grid(column = 0, row = 0)
        l2.grid(column = 0, row = 1)
        l3.grid(column = 0, row = 2)
        l4.grid(column = 0, row = 3)
        e1.grid(column = 1, row = 0, columnspan = 2)
        e2.grid(column = 1, row = 1, columnspan = 2)
        e3.grid(column = 1, row = 2, columnspan = 2)
        dropDown.grid(column = 1, row = 3, columnspan = 2)
        b1.grid(column = 1, row = 4)
        b2.grid(column = 2, row = 4)

    def connect(self):

        # Connect to database and create cursor object - used to execute queries

        self.db = pymysql.connect(host = "academic-mysql.cc.gatech.edu", db = "cs4400_Group56",
                     user = "cs4400_Group56", passwd = "pJBv9wP_")
        self.cur = self.db.cursor()

    def closeNcommit(self):

        # Close cursor/db and commit changes to db
        
        self.cur.close()
        self.db.commit()
        self.db.close()

    def insertNew(self):

        self.connect()

        # Checks to see if any entries are blank

        if self.regUsername.get() == "" or self.regPassword.get() == "" or self.regPasswordConf.get() == "":
            messagebox.showwarning("Invalid Input", "Must fill out all forms!")
            self.regUsername.set("")
            self.regPassword.set("")
            self.regPasswordConf.set("")
            self.closeNcommit()
            return

        # Checks to see if passwords match, if not - error message, set entries to blank and return

        if self.regPassword.get() != self.regPasswordConf.get():
            messagebox.showwarning("Invalid Passwords", "Passwords do not match!")
            self.regUsername.set("")
            self.regPassword.set("")
            self.regPasswordConf.set("")
            self.closeNcommit()
            return

        # Checks to see if username already used, if it is - error message, set entries to blank and return
            
        self.cur.execute("SELECT USERNAME FROM USER WHERE USERNAME = %s", (self.regUsername.get(),))
        
        for username in self.cur.fetchall():
            if self.regUsername.get() in username:
                messagebox.showwarning("Invalid Username", "Username already taken!")
                self.regUsername.set("")
                self.regPassword.set("")
                self.regPasswordConf.set("")
                self.closeNcommit()
                return

        # Insert username and password into USER
        
        self.cur.execute("INSERT INTO USER VALUES (%s, %s)", (self.regUsername.get(),self.regPassword.get()))

        # Insert username into REG_USER
        
        self.cur.execute("INSERT INTO REG_USER (REG_USERNAME) VALUES (%s)", (self.regUsername.get(),))

        # Inserts into STUDENT or FACULTY tables depending on whether student or faculty selected in drop down

        if self.userType.get() == "Student":
            self.cur.execute("INSERT INTO STUDENT (ST_USERNAME) VALUES (%s)", (self.regUsername.get(),))
        if self.userType.get() == "Faculty":
            self.cur.execute("INSERT INTO FACULTY (INST_USERNAME) VALUES (%s)", (self.regUsername.get(),))

        # Message box informing successful login

        messagebox.showinfo("Congratulations", "You have successfully registered for GTPort!")
        
        self.closeNcommit()

        # Hides register window and shows login window

        self.regWin.withdraw()
        self.loginWin.deiconify()

    def cancelRegister(self):

        self.regWin.withdraw()
        self.loginWin.deiconify()

    def login(self):

        self.connect()

        # Check to see if username exists

        self.cur.execute("SELECT PASSWORD FROM USER WHERE USERNAME = %s",(self.username.get(),))

        if self.cur.fetchall() == ():
            messagebox.showwarning("Access Denied!", "Username does not exist!")
            self.username.set("")
            self.password.set("")
            return

        # Check to see if correct password provided, if not - error message, set entries to blank, return

        self.cur.execute("SELECT PASSWORD FROM USER WHERE USERNAME = %s",(self.username.get(),))
        
        password = self.cur.fetchall()[0][0]
        if self.password.get() != password:
            messagebox.showwarning("Access Denied!", "Incorrect username/password!")
            self.username.set("")
            self.password.set("")
            return

        messagebox.showinfo("Welcome", "Welcome to GTPort, {}!".format(self.username.get()))

        # Check to see if faculty or student to call studentHome or facultyHome methods (will display proper options)

        self.cur.execute("SELECT ST_USERNAME FROM STUDENT WHERE ST_USERNAME = %s", (self.username.get(),))

        if len(self.cur.fetchall()) == 1:
            self.studentHome()
            return
            
        fac = self.cur.execute("SELECT INST_USERNAME FROM FACULTY WHERE INST_USERNAME = %s", (self.username.get(),))

        if len(self.cur.fetchall()) == 1:
            self.facultyHome()
            return

    # This function is supposed to display student homepage and make button display either personal info page
    # or student services page depending on radiobutton selected - need to work on this
        

    def studentHome(self):

        # Hide login window and show student home window
        
        self.loginWin.withdraw()
        self.studHome.deiconify()
        

        self.studHome.title("HOMEPAGE")

        self.homeStudSelect = IntVar()

        # Create widgets and format

        rb1 = Radiobutton(self.studHome, text = "Personal Information", variable = self.homeStudSelect, value = 0)
        rb2 = Radiobutton(self.studHome, text = "Student Services", variable = self.homeStudSelect, value = 1)
        logOutButton = Button(self.studHome, text = "Logout", command = self.logOut)
        b = Button(self.studHome, text = "Next", command = self.checkStudRB)   

        rb1.grid(row = 1, column = 0)
        rb2.grid(row = 2, column = 0)
        logOutButton.grid(row = 0, column = 1)
        b.grid(row = 3, column = 1)

    # This function checks to see which RB is selected on student homepage and calls the corresponding function

    def checkStudRB(self):

        if self.homeStudSelect.get() == 0:
            self.studPersonalInfo()
    

        if self.homeStudSelect.get() == 1:
            self.studServices()

    # Hides all windows and displays login window
        
    def logOut(self):

        self.studHome.withdraw()
        self.facHome.withdraw()
        self.loginWin.deiconify()

    # This function is supposed to display faculty homepage and make button display either personal info page
    # or faculty services page depending on radiobutton selected - need to work on this as well

    def facultyHome(self):

        # Hide login window and show faculty home window

        self.loginWin.withdraw()
        self.facHome.deiconify()
        

        self.facHome.title("HOMEPAGE")

        self.homeFacSelect = IntVar()

        # Creating and formatting widgets

        rb1 = Radiobutton(self.facHome, text = "Personal Information", variable = self.homeFacSelect, value = 0)
        rb2 = Radiobutton(self.facHome, text = "Faculty Services", variable = self.homeFacSelect, value = 1)
        logOutButton = Button(self.facHome, text = "Logout", command = self.logOut)
        b = Button(self.facHome, text = "Next", command = self.checkFacRB)

        rb1.grid(row = 1, column = 0)
        rb2.grid(row = 2, column = 0)
        logOutButton.grid(row = 0, column = 1)
        b.grid(row = 3, column = 1)

    # Check to see which RB selected on faculty homepage and call appropriate method

    def checkFacRB(self):
        
        if self.homeFacSelect.get() == 0:
            self.facPersonalInfo()
        if self.homeFacSelect.get() == 1:
            self.facServices()

    # These functions I have predefined because of function calls when prior buttons selected
    # These functions should display student/faculty personal information pages or student/facult services
        

    def studPersonalInfo(self):

        self.studInfoWin.deiconify()

        self.studInfoWin.title("PERSONAL INFORMATION")
        
        l1 = Label(self.studInfoWin, text = "Name")
        l2 = Label(self.studInfoWin, text = "Date of Birth")
        l3 = Label(self.studInfoWin, text = "Gender")
        l4 = Label(self.studInfoWin, text = "Address")
        l5 = Label(self.studInfoWin, text = "Permanent Address")
        l6 = Label(self.studInfoWin, text = "Contact Number")
        l7 = Label(self.studInfoWin, text = "Email Address")
        l8 = Label(self.studInfoWin, text = "Willing to tutor?")
        l9 = Label(self.studInfoWin, text = "If Yes, select the courses\nyou would like to tutor for")
        l10 = Label(self.studInfoWin, text = "Major")
        l11 = Label(self.studInfoWin, text = "Degree")

        l1.grid(column = 0, row = 0)
        l2.grid(column = 0, row = 1)
        l3.grid(column = 0, row = 2)
        l4.grid(column = 0, row = 3)
        l5.grid(column = 0, row = 4)
        l6.grid(column = 0, row = 5)
        l7.grid(column = 0, row = 6)
        l8.grid(column = 0, row = 7)
        l9.grid(column = 0, row = 8)
        l10.grid(column = 0, row = 9)
        l11.grid(column = 2, row = 9)
        
        l12 = Label(frame1, text = "PREVIOUS EDUCATION")
        l13 = Label(frame1, text = "Name of Institution attended")
        l14 = Label(frame1, text = "Major")
        l15 = Label(frame1, text = "Degree")
        l16 = Label(frame1, text = "Year of graduation")
        l17 = Label(frame1, text = "GPA")

        l12.grid(column = 0, row = 0, columnspan = 2)
        l13.grid(column = 0, row = 1)
        l14.grid(column = 0, row = 2)
        l15.grid(column = 0, row = 3)
        l16.grid(column = 0, row = 4)
        l17.grid(column = 0, row = 5)

        l18 = Label(frame2, text = "PREVIOUS EDUCATION")
        l19 = Label(frame2, text = "Name of Institution attended")
        l20 = Label(frame2, text = "Major")
        l21 = Label(frame2, text = "Degree")
        l22 = Label(frame2, text = "Year of graduation")
        l23 = Label(frame2, text = "GPA")

        l18.grid(column = 0, row = 0, columnspan = 2)
        l19.grid(column = 0, row = 1)
        l20.grid(column = 0, row = 2)
        l21.grid(column = 0, row = 3)
        l22.grid(column = 0, row = 4)
        l23.grid(column = 0, row = 5)

        l24 = Label(frame3, text = "PREVIOUS EDUCATION")
        l25 = Label(frame3, text = "Name of Institution attended")
        l26 = Label(frame3, text = "Major")
        l27 = Label(frame3, text = "Degree")
        l28 = Label(frame3, text = "Year of graduation")
        l29 = Label(frame3, text = "GPA")

        l24.grid(column = 0, row = 0, columnspan = 2)
        l25.grid(column = 0, row = 1)
        l26.grid(column = 0, row = 2)
        l27.grid(column = 0, row = 3)
        l28.grid(column = 0, row = 4)
        l29.grid(column = 0, row = 5)
        
        self.name = StringVar()
        self.DOB = StringVar()
        
        self.gender = StringVar()
        self.gender.set("Male")

        self.address = StringVar()
        self.permAddress = StringVar()
        self.contactNum = StringVar()
        self.email = StringVar()

        self.willingness = IntVar()
        
        self.tutorDropOption = StringVar()
        self.tutorEntry = StringVar()

        self.major = StringVar()
        self.degree = StringVar()

        self.prevInst1 = StringVar()
        self.prevMajor1 = StringVar()
        self.prevDegree1 = StringVar()
        self.prevYear1 = StringVar()
        self.prevGPA1 = StringVar()

        self.prevInst2 = StringVar()
        self.prevMajor2 = StringVar()
        self.prevDegree2 = StringVar()
        self.prevYear2 = StringVar()
        self.prevGPA2 = StringVar()

        self.prevInst3 = StringVar()
        self.prevMajor3 = StringVar()
        self.prevDegree3 = StringVar()
        self.prevYear3 = StringVar()
        self.prevGPA3 = StringVar()

        e1 = Entry(self.studInfoWin, textvariable = self.name)
        e2 = Entry(self.studInfoWin, textvariable = self.DOB)
        
        genderDrop = OptionMenu(self.studInfoWin, self.gender, "Male", "Female")

        e4 = Entry(self.studInfoWin, textvariable = self.address)
        e5 = Entry(self.studInfoWin, textvariable = self.permAddress)
        e6 = Entry(self.studInfoWin, textvariable = self.contactNum)
        e7 = Entry(self.studInfoWin, textvariable = self.email)

        rb1 = Radiobutton(self.studInfoWin, text = "Yes", variable = self.willingness, value = 1)
        rb2 = Radiobutton(self.studInfoWin, text = "No", variable = self.willingness, value = 2)

        #################################################################################
        ###################### PULL OUT CLASS OPTIONS FROM DB ###########################
        #################################################################################

        tutorDrop = OptionMenu(self.studInfoWin, self.tutorDropOption, "Class1", "Class2")
        
        e8 = Entry(self.studInfoWin, textvariable = self.tutorEntry)

        majorDrop = OptionMenu(self.studInfoWin, self.major, "Aerospace Engineering", "Biology", "Biomedical Engineering", "Computer Science", "Electrical & Computer Engineering")
        degreeDrop = OptionMenu(self.studInfoWin, self.degree, "BS", "MS", "Ph.D.")

        e1.grid(column = 1, row = 0, columnspan = 2)
        e2.grid(column = 1, row = 1, columnspan = 2)
        genderDrop.grid(column = 1, row = 2, columnspan = 2)
        e4.grid(column = 1, row = 3, columnspan = 2)
        e5.grid(column = 1, row = 4, columnspan = 2)
        e6.grid(column = 1, row = 5, columnspan = 2)
        e7.grid(column = 1, row = 6, columnspan = 2)

        rb1.grid(column = 1, row = 7)
        rb2.grid(column = 2, row = 7)

        tutorDrop.grid(column = 1, row = 8, columnspan = 2)
        e8.grid(column = 3, row = 8, columnspan = 2)

        majorDrop.grid(column = 1, row = 9)
        degreeDrop.grid(column = 3, row = 9)
     
        frame1 = Frame(self.studInfoWin)
        frame1.grid(column = 0, row = 10, columnspan = 4)

        e9 = Entry(frame1, textvariable = self.prevInst1)
        e10 = Entry(frame1, textvariable = self.prevMajor1)
        e11 = Entry(frame1, textvariable = self.prevDegree1)
        e12 = Entry(frame1, textvariable = self.prevYear1)
        e13 = Entry(frame1, textvariable = self.prevGPA1)

        e9.grid(column = 1, row = 0)
        e10.grid(column = 1, row = 1)
        e11.grid(column = 1, row = 2)
        e12.grid(column = 1, row = 3)
        e13.grid(column = 1, row = 4)

        frame2 = Frame(self.studInfoWin)

        e14 = Entry(frame2, textvariable = self.prevInst2)
        e15 = Entry(frame2, textvariable = self.prevMajor2)
        e16 = Entry(frame2, textvariable = self.prevDegree2)
        e17 = Entry(frame2, textvariable = self.prevYear2)
        e18 = Entry(frame2, textvariable = self.prevGPA2)

        e14.grid(column = 1, row = 0)
        e15.grid(column = 1, row = 1)
        e16.grid(column = 1, row = 2)
        e17.grid(column = 1, row = 3)
        e18.grid(column = 1, row = 4)

        frame3 = Frame(self.studInfoWin)

        e19 = Entry(frame3, textvariable = self.prevInst3)
        e20 = Entry(frame3, textvariable = self.prevMajor3)
        e21 = Entry(frame3, textvariable = self.prevDegree3)
        e22 = Entry(frame3, textvariable = self.prevYear3)
        e23 = Entry(frame3, textvariable = self.prevGPA3)

        e19.grid(column = 1, row = 0)
        e20.grid(column = 1, row = 1)
        e21.grid(column = 1, row = 2)
        e22.grid(column = 1, row = 3)
        e23.grid(column = 1, row = 4)

        b1 = Button(self.studInfoWin, text = "Add Education", command = self.addEducation)
        b2 = Button(self.studInfoWin, text = "Submit", command = self.submitStudInfo)

        b1.grid(column = 0, row = 100)
        b2.grid(column = 3, row = 100)

        

        
        

        
        
        

        
    def addEducation(self):
        pass

    def submitStudInfo(self):
        pass
        

        
    def studServices(self):
        pass

    def facPersonalInfo(self):
        
        self.facultyHome.deiconify()

        self.facInfoWin("PERSONAL INFORMATION")


        self.facName = StringVar()
        self.facDOB = StringVar()
        self.facGender = StringVar()
        self.facAddress = StringVar()
        self.facPermAddress = StringVar()
        self.facContactNum = StringVar()
        self.facEmail = StringVar()
        self.facDept = StringVar()
        self.facPosition = StringVar()
        self.facCourse = StringVar()
        self.facSection = StringVar()
        self.facResearchInterests = StringVar()
        
        l1 = Label(self.facInfoWin, text = "Name")
        l2 = Label(self.facInfoWin, text = "Date of Birth")
        l3 = Label(self.facInfoWin, text = "Gender")
        l4 = Label(self.facInfoWin, text = "Address")
        l5 = Label(self.facInfoWin, text = "Permanent Address")
        l6 = Label(self.facInfoWin, text = "Contact Information")
        l7 = Label(self.facInfoWin, text = "Email Address")
        l8 = Label(self.facInfoWin, text = "Department")
        l9 = Label(self.facInfoWin, text = "Position")
        l10 = Label(self.facInfoWin, text = "Course")
        l11 = Label(self.facInfoWin, text = "Section")
        l12 = Label(self.facInfoWin, text = "Research Interests")

        e1 = Entry(self.facInfoWin, textvariable = self.facName)
        e2 = Entry(self.facInfoWin, textvariable = self.facDOB)
        e3 = Entry(self.facInfoWin, textvariable = self.facGender)
        e4 = Entry(self.facInfoWin, textvariable = self.facAddress)
        e5 = Entry(self.facInfoWin, textvariable = self.facPermAddress)
        e6 = Entry(self.facInfoWin, textvariable = self.facContactNum)
        e7 = Entry(self.facInfoWin, textvariable = self.facEmail)
        e8 = Entry(self.facInfoWin, textvariable = self.facDept)
        e9 = Entry(self.facInfoWin, textvariable = self.facPosition)
        e10 = Entry(self.facInfoWin, textvariable = self.facCourse)
        e11 = Entry(self.facInfoWin, textvariable = self.facSection)
        e12 = Entry(self.facInfoWin, textvariable = self.facResearchInterests)

        b1 = Button(self.facInfoWin, text = "Sumbit", command = self.submitFacInfo )

        
        facGenderDrop = OptionMenu(self.facInfoWin, self.facGender, "Male", "Female")
        facDeptDrop = OptionMenu(self.facInfoWin, self.facDept, "Aerospace Engineering", "Biology", "Biomedical Engineering", "Computer Science", "Electrical & Computer Engerineering")
        facPosition = OptionMenu(self.facInfoWin, self.facPostion, "Professor", "Associate Professor", "Assistant Professor")


        facCourse = OptionMenu(self.facInfoWin, self.facCourse, "Class1", "Class2")
        facSection = OptionMenu(self.facInfoWin, self.facSection, "SectionA", "SectionB")
        ###########################################################################
        ########### PULL LIST OF COURSES AND SECTIONS THAT YOU CAN TEACH###########
        ###########################################################################

        l1.grid(row = 1, column = 0)
        l2.grid(row = 2, column = 0)
        l3.grid(row = 3, column = 0)
        l4.grid(row = 4, column = 0)
        l5.grid(row = 5, column = 0)
        l6.grid(row = 6, column = 0)
        l7.grid(row = 7, column = 0)
        l8.grid(row = 8, column = 0)
        l9.grid(row = 9, column = 0)
        l10.grid(row = 10, column = 0)
        l11.grid(row = 11, column = 0)
        l12.grid(row = 12, column = 0)
        e1.grid(row = 1, column = 1)
        e2.grid(row = 2, column = 1)
        e3.grid(row = 3, column = 1)
        e4.grid(row = 4, column = 1)
        e5.grid(row = 5, column = 1)
        e6.grid(row = 6, column = 1)
        e7.grid(row = 7, column = 1)
        e8.grid(row = 8, column = 1)
        e9.grid(row = 9, column = 1)
        e10.grid(row = 10, column = 1)
        e11.grid(row = 11, column = 1)
        e12.grid(row = 12, column = 1)
        b1.grid(row = 13, column = 2)

    def facServices(self):
        
        self.facultyHome.deiconify()

        self.facServicesWin("FACULTY SERVICES")

        l1 = Label(self.facServicesWin, text = "Assign Grades")
        l2 = Label(self.facServicesWin, text = "Update Personal Information")
        l3 = Label(self.facServicesWin, text = "Assign Tutors")
        l4 = Label(self.facServicesWin, text = "View Student Performance")

        l1.grid(row = 1, column = 0)
        l2.grid(row = 2, column = 0)
        l3.grid(row = 3, column = 0)
        l4.grid(row = 4, column = 0)

    def submitFacInfo(self):
        pass
        
            
        
win = Tk()
app = GTPort(win)
win.mainloop()
        
        

        

        
        


        
